package ATM;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;

public class SaveFrame extends JFrame {
    private Properties p = new Properties();
    private MyJText textMoney = new MyJText("请输入存款金额",50,100,this);
    private String oldMoney;

    public SaveFrame(){
        try {
            p.load(new FileReader("Housework0906/src/ATM/ATM.txt"));
        }catch (Exception e){
            System.out.println("文件不存在");
        }
        oldMoney = p.getProperty("money");


        this.setLayout(null);
        MyButton saveButton = new MyButton("存款",350,200,this);

        this.setSize(500,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setTitle("存钱");


        saveButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                saveMoney();
            }
        });

    }

    public void saveMoney(){
        int old = Integer.parseInt(oldMoney);
        int in = Integer.parseInt(textMoney.getText());
        p.setProperty("money",old+in+"");
        try {
            p.store(new FileWriter("Housework0906/src/ATM/ATM.txt"),null);
        }catch (Exception e){
            System.out.println("文件不存在");
        }
        this.dispose();
        new AskFrame();
    }

}
